package com.kaifamiao.thread.d0927;

import com.kaifamiao.demo.d0727.Student;

import java.time.LocalTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class Demo03 {

    public static void main(String[] args) {

        MyThreadFactory myThreadFactory = new MyThreadFactory();
        // 可缓存线程池, 默认 0 个核心线程，最大可创建 Integer.MAX_VALUE 个线程，当有新任务时，没有空闲线程则创建一个新的线程处理。空闲线程超过 60 秒被回收
        ExecutorService executorService = Executors.newCachedThreadPool(myThreadFactory);



//        executorService.execute(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("当前时间:" + LocalTime.now() + ", 线程: " + Thread.currentThread().getName() + ", 序号：" + number);
//            }
//        });

        for (int i = 0; i < 10; i++) {
            int number = i;
            executorService.execute(() -> {
                System.out.println("当前时间:" + LocalTime.now() + ", 线程: " + Thread.currentThread().getName() + ", 序号：" + number);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }


//        executorService.shutdown();
    }
}

class MyThreadFactory implements ThreadFactory{

    private int i = 0;

    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, "MyThreadFactory创建的第" + ++ i + "个线程");
    }
}
